<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <input type="text" id="text">
        <p id="box"></p>
    </div>
    <script>
        //定义数据
        var obj = {
            val: "好谷"
        }
        Object.defineProperty(obj, "val", {
            get: function () {
                return val
            },
            set: function (newVal) {
                console.log("val新的值---" + newVal);
                val = newVal   //重新给val赋一个新值
                document.getElementById("text").value = val  //让文本框的内容等于val
                document.getElementById("box").innerHTML = val  //让p标签中的内容等于val
            }
        })

        document.addEventListener("keyup", function (e) {
            console.log(e.target);  //<input type="text" id="text">
            obj.val = e.target.value
        })

    </script>
</body>

</html>